#!/usr/bin/python
#coding: utf-8
from sqlalchemy import *
from sqlalchemy.orm import *
engine=create_engine("sqlite:///test.db",echo=True)
metadate=MetaData(engine)

users_table=Table('users',metadate,Column('id',Integer,primary_key=True),Column('name',String),Column('password',String))
#metadate.create_all(engine)
class user(object):
    def __init__(self,name,password):
        self.name=name
        self.password=password
    def __repr__(self):
        return "<user('%s,'%s')>" %(self.name,self.password)

mapper(user,users_table)

bes_table=Table('bes',metadate,Column('id',Integer,primary_key=True),Column('name',String),Column('age',String))

class be(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def __repr__(self):
        return "<user('%s,'%s')>" %(self.name,self.password)

mapper(be,bes_table)


cpxx_table=Table('chanpin',metadate,Column('id',Integer,primary_key=True),Column('mingch',String),Column('key',String),Column('jiage',String),Column('chengben',String))
metadate.create_all(engine)
class cpxx(object):
    def __init__(self,mingch,key=None,jiage=None,chengben=None):
        self.mingch=mingch
        self.key=key
        self.jiage=jiage
        self.chengben=chengben
    def __repr__(self):
        return "<cpxx('%s,'%s','%s','%s')>" %(self.mingch,self.key,self.jiage,self.chengben)

mapper(cpxx,cpxx_table)



kehuxx_table=Table('kehu',metadate,Column('id',Integer,primary_key=True),Column('name',String),Column('phone',String),Column('addr',String),Column('gsname',String))
metadate.create_all(engine)
class kehuxx(object):
    def __init__(self,name,phone=None,addr=None,gsname=None):
        self.name=name
        self.phone=phone
        self.addr=addr
        self.gsname=gsname
    def __repr__(self):
        return "<kehuxx('%s,'%s','%s','%s')>" %(self.name,self.phone,self.addr,self.gsname)

mapper(kehuxx,kehuxx_table)


metadate.create_all(engine)

def CreateSession():
    return Session()
def CloseSession(session):
    session.close()
def CommitSession(session):
    session.commit() 
def SelectOb(curob):
    s=Session()
    lists=s.query(curob).all()
    s.close()
    return lists
def InsertOb(curob):
    s=Session()
    s.add(curob)
    s.commit()
    s.close()
def DeleteOb(curob,q1='name',val='ye'):
    s=Session()
    print '---'*8
    print q1
    print val
    print '---'*8
    tempob=s.query(curob).filter_by(**{q1:val}).first()
    s.delete(tempob)
    s.commit()
    s.close()
#DeleteOb(user)

"""for i in range(2):
    insql=user(u'ye','b')
    s=Session()
    s.add(insql)
    s.commit()
    s.close()
"""